/**
 * 
 */
package com.kylin.frame.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.util.Assert;

/**
 * web常用类
 * 
 * @author huxiang
 * 
 */
public class WebUtils {

	/**
	 * Check the given request for a session attribute of the given name.
	 * Returns null if there is no session or if the session has no such
	 * attribute. Does not create a new session if none has existed before!
	 * 
	 * @param request
	 *            current HTTP request
	 * @param name
	 *            the name of the session attribute
	 * @return the value of the session attribute, or <code>null</code> if not
	 *         found
	 */
	public static Object getSessionAttribute(HttpServletRequest request,
			String name) {
		Assert.notNull(request, "Request must not be null");
		HttpSession session = request.getSession(false);
		return (session != null ? session.getAttribute(name) : null);
	}

	/**
	 * 判断是否为Ajax请求
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return 是true, 否false
	 */
	public static boolean isAjaxRequest(HttpServletRequest request) {
		String requestType = request.getHeader("X-Requested-With");
		if (requestType != null && requestType.equals("XMLHttpRequest")) {
			return true;
		} else {
			return false;
		}
	}
}
